package com.nbstar.film.spider.excel;

import com.nbstar.film.spider.domain.FilmSpiderDomain;
import com.nbstar.film.spider.pojo.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public class SiChuanExcelWriter extends XSSExcelWriter {

    private static Logger logger = LoggerFactory.getLogger(SiChuanExcelWriter.class);


    public SiChuanExcelWriter(String sheetName, String excelPath) {
        super(sheetName, excelPath);
    }

    @Override
    public ExcelVedioInfo trans2Excel(FilmSpiderDomain domain) {
        FilmExcelInfo filmExcelInfo = new FilmExcelInfo();
        DouBanFilmInfo douBanFilmInfo = (DouBanFilmInfo) domain.get(FilmSpiderDomain.SPIDER_FROM_DOUBAN);
        MaoYanFilmInfo maoyanFilmInfo = (MaoYanFilmInfo) domain.get(FilmSpiderDomain.SPIDER_FROM_MAOYAN);
        FilmInfo imdbFilmInfo = domain.get(FilmSpiderDomain.SPIDER_FROM_IMDB);
        FilmInfo tvCatFilmInfo = domain.get(FilmSpiderDomain.SPIDER_FROM_TVMAO_TV);
        FilmInfo bkFilmInfo = domain.get(FilmSpiderDomain.SPIDER_FROM_BAIKE);

        if (maoyanFilmInfo != null) {
            filmExcelInfo.setBoxOffice(maoyanFilmInfo.getBoxOffice());
            filmExcelInfo.setMaoyanScore(maoyanFilmInfo.getAverage());
            filmExcelInfo.setMaoyanVotes(maoyanFilmInfo.getVotes());
            if (!StringUtils.isEmpty(maoyanFilmInfo.getCategory())) {
                filmExcelInfo.setCategory(maoyanFilmInfo.getCategory());
            }

            if (!StringUtils.isEmpty(maoyanFilmInfo.getArriveAddress())) {
                filmExcelInfo.setArriveAddress(maoyanFilmInfo.getArriveAddress());
            }

            if (!StringUtils.isEmpty(maoyanFilmInfo.getArriveDate())) {
                filmExcelInfo.setArriveDate(maoyanFilmInfo.getArriveDate());
            }

            if (maoyanFilmInfo.isSuccess()) {
                filmExcelInfo.setMaoyanResult("成功");
            }


        }

        if (tvCatFilmInfo != null) {

            filmExcelInfo.setEpisode(tvCatFilmInfo.getPartNum());
            filmExcelInfo.setOnlinePlatform(tvCatFilmInfo.getOnlinePlatform());
            if (tvCatFilmInfo.isSuccess()) {
                filmExcelInfo.setTvCatResult("成功");
            }

        }

        if (douBanFilmInfo != null) {
            filmExcelInfo.setDoubanScore(douBanFilmInfo.getAverage());
            if (!StringUtils.isEmpty(douBanFilmInfo.getArriveAddress())) {
                filmExcelInfo.setArriveAddress(douBanFilmInfo.getArriveAddress());
            }
            if (!StringUtils.isEmpty(douBanFilmInfo.getArriveDate())) {
                filmExcelInfo.setArriveDate(douBanFilmInfo.getArriveDate());
            }
            filmExcelInfo.setDoubanVotes(douBanFilmInfo.getVotes());
            if (!StringUtils.isEmpty(douBanFilmInfo.getCategory())) {
                filmExcelInfo.setCategory(douBanFilmInfo.getCategory());
            }

            if (douBanFilmInfo.isSuccess()) {
                filmExcelInfo.setDoubanResult("成功");
            }

            filmExcelInfo.setOnlinePlatform(douBanFilmInfo.getOnlinePlatform());

        }

        if (imdbFilmInfo != null) {
            filmExcelInfo.setImdbScore(imdbFilmInfo.getAverage());
            filmExcelInfo.setImdbVotes(imdbFilmInfo.getVotes());
            if (imdbFilmInfo.isSuccess()) {
                filmExcelInfo.setImdbResult("成功");
            }

        }

        //TODO 从电视猫上面获取是否院线上线
        if (!StringUtils.isEmpty(filmExcelInfo.getArriveDate())) {
            filmExcelInfo.setOnCinema("是");
        } else {
            filmExcelInfo.setOnCinema("否");
        }


        if (bkFilmInfo != null) {
            filmExcelInfo.setPartNum(bkFilmInfo.getPartNum());
            if (bkFilmInfo.isSuccess()) {
                filmExcelInfo.setBaikeResult("成功");
            }
        }

        filmExcelInfo.setBrightSpot(domain.getRaw().getBrightSpot());
        filmExcelInfo.setCopyrightNumber(domain.getRaw().getCopyrightNumber());
        filmExcelInfo.setCopyrightValidDate(domain.getRaw().getCopyrightValidDate());

        filmExcelInfo.setFilmName(domain.getRaw().getFilmName());
        filmExcelInfo.setSeqNo(domain.getRaw().getSeqNo());



        return filmExcelInfo;
    }

    @Override
    protected void initExcelHeader(Row r) {
        r.createCell(0).setCellValue("公司名称");
        r.createCell(1).setCellValue("序号");
        r.createCell(2).setCellValue("节目集名称");
        r.createCell(3).setCellValue("节目集类型");	//给表头定义字段名
        r.createCell(4).setCellValue("制片年份");	//给表头定义字段名
        r.createCell(5).setCellValue("是否院线上映");
        r.createCell(6).setCellValue("票房（亿)");
        r.createCell(7).setCellValue("豆瓣评分");
        r.createCell(8).setCellValue("IMDb评分");
        r.createCell(9).setCellValue("猫眼评分");
        r.createCell(10).setCellValue("综合评分");
        r.createCell(11).setCellValue("豆瓣参评人数");
        r.createCell(12).setCellValue("IMDb参评人数");
        r.createCell(13).setCellValue("猫眼参评人数");
        r.createCell(14).setCellValue("综合参评");
        r.createCell(15).setCellValue("预打分数");
        r.createCell(16).setCellValue("集数");
        r.createCell(17).setCellValue("版权号");
        r.createCell(18).setCellValue("版权有效期");
        r.createCell(19).setCellValue("内容亮点");
        r.createCell(20).setCellValue("二级分类");
        r.createCell(21).setCellValue("播出平台");
        r.createCell(22).setCellValue("豆瓣抓取结果");
        r.createCell(23).setCellValue("IMDB抓取结果");
        r.createCell(24).setCellValue("猫眼抓取结果");
        r.createCell(25).setCellValue("电视猫抓取结果");
        r.createCell(26).setCellValue("百科抓取结果");
        r.createCell(27).setCellValue("播出平台");

    }

    @Override
    protected void writeRow(Row r, FilmExcelInfo filmDomain) {
        r.createCell(0).setCellValue(filmDomain.getCompany());
        r.createCell(1).setCellValue(filmDomain.getSeqNo());
        r.createCell(2).setCellValue(filmDomain.getFilmName());
        r.createCell(3).setCellValue(filmDomain.getType());
        r.createCell(4).setCellValue(filmDomain.getArriveDate());
        r.createCell(5).setCellValue(filmDomain.getOnCinema());
        ;
        r.createCell(6).setCellValue(filmDomain.getBoxOffice());
        r.createCell(7).setCellValue(filmDomain.getDoubanScore());
        r.createCell(8).setCellValue(filmDomain.getImdbScore());
        r.createCell(9).setCellValue(filmDomain.getMaoyanScore());
        r.createCell(10).setCellValue((filmDomain.getDoubanScore() + filmDomain.getImdbScore() + filmDomain.getMaoyanScore()) / 3);
        r.createCell(11).setCellValue(filmDomain.getDoubanVotes());
        r.createCell(12).setCellValue(filmDomain.getImdbVotes());
        r.createCell(13).setCellValue(filmDomain.getMaoyanVotes());
        r.createCell(14).setCellValue((filmDomain.getImdbVotes() + filmDomain.getDoubanVotes() + filmDomain.getMaoyanVotes()) / 3);

        boolean isChinaFilm = false;
        if (filmDomain.getArriveAddress() != null) {
            if (filmDomain.getArriveAddress().contains("中国")
                    || filmDomain.getArriveAddress().contains("大陆")
                    || filmDomain.getArriveAddress().contains("内地")
                    || filmDomain.getArriveAddress().contains("内陆")
                    || filmDomain.getArriveAddress().contains("香港")
                    || filmDomain.getArriveAddress().contains("台湾")) {
                isChinaFilm = true;
            }
        }
        //TODO 预打分，无法拿到从院线下线时间
        r.createCell(15).setCellValue(0 );

        r.createCell(16).setCellValue(filmDomain.getPartNum());
        r.createCell(17).setCellValue(filmDomain.getCopyrightNumber());
        r.createCell(18).setCellValue(filmDomain.getCopyrightValidDate());
        r.createCell(19).setCellValue(filmDomain.getBrightSpot());
        r.createCell(20).setCellValue(filmDomain.getCategory());
        r.createCell(21).setCellValue(filmDomain.getOnlinePlatform());
        r.createCell(22).setCellValue(filmDomain.getDoubanResult());
        r.createCell(23).setCellValue(filmDomain.getImdbResult());
        r.createCell(24).setCellValue(filmDomain.getMaoyanResult());
        r.createCell(25).setCellValue(filmDomain.getTvCatResult());
        r.createCell(26).setCellValue(filmDomain.getBaikeResult());
        r.createCell(27).setCellValue(filmDomain.getOnlinePlatform());
    }


}
